package com.googlecode.montelin.server;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.com.google.common.base.Charsets;
import com.google.appengine.repackaged.com.google.common.collect.Lists;
import com.google.appengine.repackaged.com.google.common.io.Files;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.googlecode.montelin.server.dao.DAO;
import com.googlecode.montelin.server.domain.Address;
import com.googlecode.montelin.server.domain.Configuration;
import com.googlecode.montelin.server.domain.Page;
import com.googlecode.montelin.server.domain.User;

@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String lang = Constants.DEFAULT_LANGUAGE;
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		Injector inj = Guice.createInjector(new Module());
		DAO dao = inj.getInstance(DAO.class);

		// User
		User earl = dao.findUserByName(Constants.DEFAULT_USER);
		if(earl==null) {
			earl = new User(Constants.DEFAULT_USER);
		}
		earl.setAddress(new Address("Domaine de Montelin",17250,"Sainte GEMME"));
		earl.setFax("+33 6 67 95 53 47");
		earl.setMail("thierry.chartier@domaine-de-montelin.com");
		earl.setMobilePhone("+33 6 30 10 10 10");
		earl.setPhone("+33 5 46 22 95 85");
		earl = dao.persist(earl);
		
		// Configuration
		Configuration fr = dao.findConfigurationByLang(lang);
		if(fr==null) {
			fr = new Configuration();
		}
		
		
		fr.setLang(lang);
		fr.setAuthor("Romain CHARTIER");
		fr.setCopyright("EARL Chartier");
		String[] keywords = "pineau,cognac,charentes,charentes-maritime,terroir,montelin,domaine,domaine de montelin,vigne,vignoble,pineau rosé,pineau blanc,chartier thierry,chartier".split(",");
		fr.setKeywords(Lists.asList(keywords[0],keywords));
		fr.setRating("general");
		fr.setRobots("index,follow");
		fr.setLang("fr");
		fr.setSlogan("Une tradition à la maison, Pineau des charentes à l'apéritif!");
		fr = dao.persist(fr);
		
		// Pages
		Page root = dao.findPageByCodeAndLang(Constants.ROOT_CODE, lang);
		if(root==null) {
			root = new Page(Constants.ROOT_CODE, lang);
		}
		root = dao.persist(root);
		
		Page p;
		String code;
		// - Accueil
		code = Constants.DEFAULT_CODE;
		p = dao.findPageByCodeAndLang(code, lang);
		
		if(p==null) {
			p = new Page(code, lang);
		}
		
		p.setDescription("Pineau des charentes et cognac du domaine de montelin. Nous presentons donc nos produits et les differentes méthodes de vinifications.");
		p.setKeywords(null);
		p.setMenuCaption("Accueil");
		p.setPosition(1);
		p.setParent(root);
		p.setTitle("Bienvenue!!! Le Domaine de Montelin vous ouvre ses portes. Pineau des charentes et cognac de grande qualité vous attendent!");
		p.setContent(loadFile("accueil"));
		
		p = dao.persist(p);
		
		// - products
		code = "products";
		p = dao.findPageByCodeAndLang(code, lang);
		
		if(p==null) {
			p = new Page(code, lang);
		}
		
		p.setDescription("Liste de nos produits: pineau des charentes et cognac");
		p.setKeywords(null);
		p.setMenuCaption("Nos Produits");
		p.setPosition(2);
		p.setParent(root);
		p.setTitle("Nos produits");
		p.setContent(loadFile("produits"));
		p = dao.persist(p);

		// - products/pineau
		code = "pineau";
		Page p1 = dao.findPageByCodeAndLang(code, lang);
		
		if(p1==null) {
			p1 = new Page(code, lang);
		}
		
		p1.setDescription("Pineau des charentes: Blanc ou Rosé");
		p1.setKeywords(null);
		p1.setMenuCaption("Pineau des charentes");
		p1.setPosition(1);
		p1.setParent(p);
		p1.setTitle("Pineau des charentes");
		p1.setContent(loadFile("pineauList"));
		p1 = dao.persist(p1);

		// - products/cognac
		code = "cognac";
		p1 = dao.findPageByCodeAndLang(code, lang);
		
		if(p1==null) {
			p1 = new Page(code, lang);
		}
		
		p1.setDescription("Cognac");
		p1.setKeywords(null);
		p1.setMenuCaption("Cognac");
		p1.setPosition(1);
		p1.setParent(p);
		p1.setTitle("Cognac");
		p1.setContent(loadFile("cognacList"));
		p1 = dao.persist(p1);

	}

	private String loadFile(String file) throws IOException {
		return Files.toString(new File("content/"+file), Charsets.UTF_8);
	}

}
